/*
 * @Author: 'Huaiantree' '2577850557@qq.com'
 * @Date: 2025-09-15 19:22:14
 * @LastEditors: 'Huaiantree' '2577850557@qq.com'
 * @LastEditTime: 2025-10-15 02:19:50
 * @FilePath: \easyTool-client\src\router\index.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
// src/router/index.ts
import { createRouter, createWebHistory } from 'vue-router';
import Login from '../views/Login.vue';
import Home from '../views/Home.vue';
import Detail from '../views/Detail.vue';
import Detail2 from '../views/Detail2.vue';

function isLoggedIn() {
  const expiration = localStorage.getItem('loginExpiration');
  if (!expiration) return false;
  return new Date().getTime() < parseInt(expiration, 10);
}

const routes = [
  { path: '/login', component: Login },
  { path: '/home', component: Home },
  { path: '/detail/:mid', component: Detail },
  { path: '/detail2/:mid', component: Detail2 },
  { path: '/', redirect: '/home' }
];

const router = createRouter({
  history: createWebHistory(),
  routes
});

// 添加全局前置守卫
router.beforeEach((to, _from, next) => {
  if (!isLoggedIn() && to.path !== '/login') {
    next('/login'); // 重定向到登录页面
  } else {
    next(); // 继续导航
  }
});

export default router;
